package controllers

import (
	"cronjob/models"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"
)

// 资源分组管理
type ServerGroupController struct {
	BaseController
}

func (this *ServerGroupController) List() {
	this.Data["pageTitle"] = "资源管理"
	this.display()
}

func (this *ServerGroupController) Table() {
	page, err := this.GetInt("page")
	if err != nil {
		page = 1
	}

	//每页显示几个
	limit, err := this.GetInt("limit")
	if err != nil {
		limit = 5
	}

	this.pageSize = limit
	filters := make([]interface{}, 0)
	filters = append(filters, "status", 1)

	groupName := strings.TrimSpace(this.GetString("groupName"))

	if groupName != "" {
		filters = append(filters, "group_name__contains", groupName)
	}

	// 不是超级管理员
	if this.userID != 1 {
		groups := strings.Split(this.serverGroups, ",")

		groupIds := make([]int, 0)
		for _, v := range groups {
			id, _ := strconv.Atoi(v)

			groupIds = append(groupIds, id)
		}
		filters = append(filters, "id__in", groupIds)
	}

	result, count := models.ServerGroupGetList(page, this.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))

	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["group_name"] = v.GroupName
		row["description"] = v.Description
		row["create_time"] = beego.Date(time.Unix(v.CreateTime, 0), "Y-m-d H:i;s")
		row["update_time"] = beego.Date(time.Unix(v.UpdateTime, 0), "Y-m-d H:i;s")
		list[k] = row
	}

	this.ajaxList("", MSG_OK, count, list)
}

func (this *ServerGroupController) Add() {
	this.Data["pageTitle"] = "新增分组"
	this.Data["hideTop"] = true

	this.display()
}

func (this *ServerGroupController) Edit() {

	this.Data["pageTitle"] = "编辑分组"
	this.Data["hideTop"] = true

	group_id, _ := this.GetInt("id")
	group, err := models.TaskGroupGetById(group_id)

	if err != nil {
		this.ajaxMsg("服务分组不存在", MSG_ERR)
	}

	row := make(map[string]interface{})
	row["id"] = group.Id
	row["group_name"] = group.GroupName
	row["description"] = group.Description

	this.Data["group"] = row
	this.display()
}

func (this *ServerGroupController) AjaxSave() {
	serverGroup := new(models.ServerGroup)
	serverGroup.GroupName = strings.TrimSpace(this.GetString("group_name"))
	serverGroup.Description = strings.TrimSpace(this.GetString("description"))
	serverGroup.Status = 1

	servergroup_id, _ := this.GetInt("id")

	if servergroup_id == 0 {
		//新增
		serverGroup.CreateTime = time.Now().Unix()
		serverGroup.UpdateTime = time.Now().Unix()
		serverGroup.CreateId = this.userID
		serverGroup.UpdateId = this.userID

		if _, err := models.ServerGroupAdd(serverGroup); err != nil {
			this.ajaxMsg("", MSG_ERR)
		}
		this.ajaxMsg("添加成功", MSG_OK)
	}

	serverGroup.Id = servergroup_id
	serverGroup.UpdateTime = time.Now().Unix()
	serverGroup.UpdateId = this.userID

	if ok := serverGroup.Update(); ok != nil {
		this.ajaxMsg("", MSG_ERR)
	}
	this.ajaxMsg("修改失败", MSG_OK)

}

func (this *ServerGroupController) AjaxDel() {
	group_id, _ := this.GetInt("id")
	group, err := models.TaskGroupGetById(group_id)

	if err != nil {
		this.ajaxMsg("服务分组不存在", MSG_ERR)
	}
	group.Status = 0
	group.CreateId = this.userID
	group.CreateTime = time.Now().Unix()

	// 如果分组下有正常的任务不能设置
	filters := make([]interface{}, 0)
	filters = append(filters, "status", 0)
	filters = append(filters, "group_id", group_id)
	_, count := models.TaskGetList(1, 100, filters...)

	if count > 0 {
		this.ajaxMsg("服务分组下有可用任务", MSG_ERR)
	}

	if ok := group.Update(); ok != nil {
		this.ajaxMsg("服务分组设置失败", MSG_ERR)
	}

	this.ajaxMsg("设置成功", MSG_OK)
}
